/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.io.*;
import java.net.*;

/**
 *
 * @author sun
 */
public class sev {
      public static void main(String[] args) {
        // TODO code application logic here
        ServerSocket svr = null;
        Socket soc = null;
        InputStream is = null;
        OutputStream os = null;
        try {
            svr = new ServerSocket(6112); // ServerSocket对象，监听指定的端口
            while (true) {
                soc = svr.accept(); 		// 建立连接
                is = soc.getInputStream(); 	// 输入流的获取
                os = soc.getOutputStream(); 	// 输出流的获取
                BufferedReader in = new BufferedReader(new InputStreamReader(is));
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(os));
                System.out.println("客户地址:" + soc.getInetAddress());
                System.out.println("客户端口:" + soc.getPort());
                System.out.println("欢迎访问!..."); 		// 通知客户连接成功
                out.write("欢迎访问!...\r\n");
                out.flush();
                while (true) {
                    String str = in.readLine();			 // 接收客户一行数据
                    System.out.println("收到:" + str);	 	// 输出到屏幕
                    if (str.equals("quit")) {
                        break;
                    }
                }
                System.out.println("对话结束.");
                is.close();
                os.close();
                soc.close();
            }
        } catch (Exception e) {
            System.out.println("出错:" + e);
        }



    }
}
